﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaskX
{
    public class TaskStorage
    {
        private SortedList<TimedTask, int> timedTask;
        private SortedList<FloatingTask, int> floatingTask;
        private SortedList<DeadlineTask, int> deadlineTask;
        private int floatCounter = 0;
        private class TimedComparer : IComparer<TimedTask>
        {
            // comparer class 
            public int Compare(TimedTask x, TimedTask y)
            {
                if (x.GetStartTime().date > y.GetStartTime().date)
                    return 1;
                else
                    if (x.GetStartTime().date == y.GetStartTime().date)
                    {
                        if (x.GetStartTime().time > y.GetStartTime().time)
                            return 1;
                        else
                            if (x.GetStartTime().time == y.GetStartTime().time)
                            {
                                if (x.GetTasks().CompareTo(y.GetTasks()) > 0)
                                    return 1;
                                else
                                    if (x.GetTasks() == y.GetTasks())
                                        return 0;
                                    else
                                        return -1;
                            }
                            else
                                return -1;
                    }
                    else
                        return -1;

            }

        }

        private class DeadlineComparer : IComparer<DeadlineTask>
        {
            public int Compare(DeadlineTask x, DeadlineTask y)
            {
                if (x.GetEndTime().date > y.GetEndTime().date)
                    return 1;
                else
                    if (x.GetEndTime().date == y.GetEndTime().date)
                    {
                        if (x.GetEndTime().time > y.GetEndTime().time)
                            return 1;
                        else
                            if (x.GetEndTime().time == y.GetEndTime().time)
                            {
                                if (x.GetTasks().CompareTo(y.GetTasks()) > 0)
                                    return 1;
                                else
                                    if (x.GetTasks() == y.GetTasks())
                                        return 0;
                                    else
                                        return -1;
                            }
                            else
                                return -1;
                    }
                    else
                        return -1;

            }

        }

        private class FloatComparer : IComparer<FloatingTask>
        {

            // comparer class 
            public int Compare(FloatingTask x, FloatingTask y)
            {
                if (x.GetOrder() > y.GetOrder())
                    return 1;
                else
                    if (x.GetOrder() == y.GetOrder())
                        return 0;
                    else
                        return -1;

            }

        }

        public TaskStorage()
        {
            timedTask = new SortedList<TimedTask, int>(new TimedComparer());
            deadlineTask = new SortedList<DeadlineTask, int>(new DeadlineComparer());
            floatingTask = new SortedList<FloatingTask, int>(new FloatComparer());
        }

        public void InsertTimedTask(TimedTask a)
        {
            timedTask.Add(a, 0);
        }

        public void InsertDeadlineTask(DeadlineTask a)
        {
            deadlineTask.Add(a, 0);
        }

        public void InsertFloatingTask(FloatingTask a)
        {
            floatCounter++;
            a.SetOrder(floatCounter);
            floatingTask.Add(a, 0);
        }

        //have to consider same time date and time cases
        public bool RemoveTimedTask(TimedTask a)
        {
            if (timedTask.ContainsKey(a))
            {
                timedTask.Remove(a);
                return true;
            }
            return false;
        }

        public bool RemoveDeadlineTask(DeadlineTask a)
        {
            if (deadlineTask.ContainsKey(a))
            {
                deadlineTask.Remove(a);
                return true;
            }
            return false;
        }

        public bool RemoveFloatingTask(FloatingTask a)
        {
            if (floatingTask.ContainsKey(a))
            {
                floatingTask.Remove(a);
                return true;
            }
            return false;
        }

        public void MarkTimedTask(TimedTask a)//mark 1 to the task to show that it is marked as done
        {
            timedTask[a] = 1;
        }

        public void MarkDeadlineTask(DeadlineTask a)
        {
            deadlineTask[a] = 1;
        }

        public void MarkFloatingTask(FloatingTask a)
        {
            floatingTask[a] = 1;
        }

        public void search()
        {
        }


    }
}
